﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MyTestProject
{
    class Document : ICheckable
    {
        int _id;
        string _text;
        int _maxFrequency;
        Dictionary<string, double> _tf; 

        public Document(string text)
        {
            _id = IDMaker.GetUID();
            _text = text.ToLower();
            _maxFrequency = 0;
            _tf = new Dictionary<string, double>();
            GenerateTF();
        }

        private void GenerateTF()
        {
            string[] docArray = _text.Split(' ');
            foreach (string word in docArray)
            {
                if (_tf.ContainsKey(word))
                {
                    _tf[word]++;
                }
                else
                {
                    _tf[word] = 1;
                }

                if (_tf[word] > _maxFrequency)
                {
                    _maxFrequency = (int)_tf[word];
                }
            }

            List<string> keys = _tf.Keys.ToList();
            foreach (string key in keys)
            {
                _tf[key] /= _maxFrequency;
            }
        }

        public string Text
        {
            get { return _text; }
        }

        int MaxFrequency
        {
            get { return _maxFrequency; }
        }

        public int NumberOfOccurrences(string word)
        {
            int ans;
            if (!_text.Split(' ').Contains(word))
            {
                return 0;
            }
            else
            {
                ans = 0;
                string[] strArray = _text.Split(' ');
                for (int i = 0; i < strArray.Length; i++)
                {
                    if (strArray[i].Equals(word))
                    {
                        ans++;
                    }
                }
            }
            return ans;
        }

        public Dictionary<string, double> TF
        {
            get { return _tf; }
        }

        public override string ToString()
        {
            string ans = _text + ": ";
            foreach (KeyValuePair<string, double> pair in _tf)
            {
                ans += Environment.NewLine + "--" + pair.Key + ": " + pair.Value;
            }
            return ans;
        }

        public Boolean Contains(string word)
        {
            if (_text.Split(' ').Contains(word))
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public int ID
        {
            get { return _id; }
        }
    }
}
